﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Webadmin_Modules_Branding_BrandBody : bzAdminUCBase
{
    public string _Act = "";
    public DataTable _dt = new DataTable();
    public bzShopAddress bz = new bzShopAddress();
    private string _CateID = "-1";
    override protected void loadAction()
    {
        _Act = Request.QueryString["Act"] == null ? "" : Request.QueryString["Act"];
        _CateID = Request.QueryString["CateID"] == null ? "-1" : Request.QueryString["CateID"];
        if (!IsPostBack)
        {
            this.loadFRMFilter();
            switch (_Act)
            {
                case "Dis": this.active("0"); break;
                case "Ena": this.active("1"); break;
                case "Del": this.delete(); break;
                case "Save": this.save(); break;
                case "Up": this.up_down(true); break;
                case "Down": this.up_down(false); break;
                case "Home": this.changeHome(); break;
            }
            this.showData();
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        this.initSiteLang(bzSiteOption.Group.Administrator, bzSiteOption.Note.AddShop, "ShopAddress");
    }
  
    private void changeHome()
    {
        if (this.AllowEdit)
        {
            string _ID = string.IsNullOrEmpty(Request.QueryString["ID"]) ? "1" : Request.QueryString["ID"];
            bz.changeHome(_ID);
        }
        else
        {
            Response.Redirect(bzStringClass.AlertPermission);
        }

    }
    private void delete()
    {
        if (this.AllowDelete)
        {
            string chk = Request.Form["Chk"] == null ? "-1" : Request.Form["Chk"];
            string[] sID = chk.Split(',');
            string[] arrPicThumb = new string[sID.Length];
            string[] arrPicFull = new string[sID.Length];
            string path = Server.MapPath("~") + "/Images/AddShop/";
            for (int i = 0; i < sID.Length; i++)
            {
                if (string.IsNullOrEmpty(sID[i])) continue;
                DataTable dtTmp = bz.getRecord(sID[i], "-1");
                if (dtTmp.Rows.Count > 0)
                {
                    arrPicThumb[i] = dtTmp.Rows[0]["PicThumb"].ToString();
                    arrPicFull[i] = dtTmp.Rows[0]["PicFull"].ToString();
                }               
            }
            int count = bz.deleteRecord(sID);
            if (count >= sID.Length)
            {
                for (int i = 0; i < arrPicFull.Length; i++)
                {
                    string[] sPicThumb = arrPicThumb[i].Split(';');
                    string[] sPicFull = arrPicFull[i].Split(';');
                    for (int j = 0; j < sPicFull.Length; j++)
                    {
                        if (sPicFull[j] != String.Empty)
                        {
                            bzFile.delete(path + sPicThumb[j]);
                            bzFile.delete(path + sPicFull[j]);
                        }
                    }
                }

            }
            else
            {
                Response.Write(bzStringClass.addStringAlert(this.AdminLang["ChildrenNotEmpty"].ToString()));
                Response.Redirect(Session["Page"].ToString());
            }
        }
        else Response.Write(bzStringClass.AlertPermission);
    }


    private void active(string act)
    {
        if (this.AllowEdit)
        {
            string chk = Request.Form["Chk"] == null ? "-1" : Request.Form["Chk"];
            bz.activeRecord(chk.Split(','), act);
        }
        else { Response.Redirect(bzStringClass.AlertPermission); }
    }

    private void up_down(bool isUp)
    {
        if (this.AllowEdit)
        {
            string ID = Request.QueryString["ID"] == null ? "-1" : Request.QueryString["ID"];
            if (isUp)
                bz.upOrder(ID);
            else
                bz.downOrder(ID);

        }
        else { Response.Redirect(bzStringClass.AlertPermission); }
    }
    private void save()
    {
        if (this.AllowEdit)
        {
            string chk = Request.Form["Chk"] == null ? "-1" : Request.Form["Chk"];
            string order = Request.Form["txtOrder"] == null ? "-1" : Request.Form["txtOrder"];
            bz.saveOrder(chk.Split(','), order.Split(','));
        }
        else { Response.Redirect(bzStringClass.AlertPermission); }
    }
    private void loadFRMFilter()
    {
      
        this.cboFilterEnabled.Items.Add(new ListItem(this.AdminLang["All"].ToString(), "-1"));
        this.cboFilterEnabled.Items.Add(new ListItem(this.AdminLang["Enabled"].ToString(), "1"));
        this.cboFilterEnabled.Items.Add(new ListItem(this.AdminLang["Disabled"].ToString(), "0"));

        this.cboFilterEnabled.SelectedValue = Request.QueryString["Enabled"] == null ? "-1" : Request.QueryString["Enabled"];
        this.txtFilter.Text = Request.QueryString["Keyword"] == null ? "" : Request.QueryString["Keyword"];
        this.btnFilter.Text = this.AdminLang["Filter"].ToString();
    }
    protected void btnFilter_Click(object sender, EventArgs e)
    {
        this.showData();
    }
    private void showData()
    {
        this.SearchString = "&Keyword=" + this.txtFilter.Text + "&Enabled=" + this.cboFilterEnabled.SelectedValue;
        this.loadPagination(bz.getTotal(this.txtFilter.Text.Replace('*', '%'), this.cboFilterEnabled.SelectedValue, "-1", "-1","-1","-1"), Convert.ToInt32(this.SiteOption["AdminLimit"]), "?" + this.SearchString);
        _dt = bz.getTable(this.txtFilter.Text.Replace('*', '%'), this.cboFilterEnabled.SelectedValue, this.CurrentPage.ToString(), this.SiteOption["AdminLimit"].ToString(), "-1", "-1","-1","-1");
        this.Navigator = this.AdminLang["MenuBranding"].ToString() + this.AdminLang["GeneralArrow"].ToString() + this.AdminLang["MenuBrandingList"].ToString();
        this.SearchString += "&Page=" + this.CurrentPage;
    }
}
